﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelperPrototype._DB
{
    class QuyTrinhMuaHangDB
    {
        public static IQueryable selectAll(_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.QuyTrinhMuaHangs
                        where u.deleted ==false
                        select u;
            return query;
        }
        public static QuyTrinhMuaHang selectID(int id,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.QuyTrinhMuaHangs
                        where u.ID==id
                        select u ;
            if (query.Count()>0)
            {
                return query.First();
            }
            return null;
            //return query;
        }
        public static bool insert(_DB.QuyTrinhMuaHang u,_DB.DataClasses1DataContext db)
        {
            if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            db.QuyTrinhMuaHangs.InsertOnSubmit(u);
            db.SubmitChanges();
            return true;
        }
        public static void update(QuyTrinhMuaHang newQuyTrinh,_DB.DataClasses1DataContext db)
        {
             if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = (from u in db.QuyTrinhMuaHangs
                        where u.ID == newQuyTrinh.ID
                        select u);
          //  MessageBox.Show(newQuyTrinh.MaPhieuDeNghiMuaHang1.ToString()+" "+query.Count().ToString());
            if (query.Count() > 0)
            {
                QuyTrinhMuaHang q = query.First();
                
                q.TrangThai = newQuyTrinh.TrangThai;
                q.MaPhieuDeNghiMuaHang1 = newQuyTrinh.MaPhieuDeNghiMuaHang1;
                q.MaPhieuDeNghiMuaHang2 = newQuyTrinh.MaPhieuDeNghiMuaHang2;
                q.deleted = newQuyTrinh.deleted;
                db.SubmitChanges();
              //  return query.First();
            }
            return ;
            //return query;
        }
        public static void delete(int id,_DB.DataClasses1DataContext db)
        {
             if (db == null)
            {
                db = new _DB.DataClasses1DataContext();
            }
            var query = from u in db.QuyTrinhMuaHangs
                        where u.ID == id
                        select u;
            if (query.Count() > 0)
            {
                QuyTrinhMuaHang q = query.First();
                q.deleted = true;
                db.SubmitChanges();
            }
            return ;
            //return query;
        }
    }
    
}
